<template>
  <!-- 添加或修改发票管理对话框 -->
  <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body :close-on-click-modal="false" v-dialogDrag>
    <el-form :model="form" :rules="rules" label-width="120px" ref="form">
      <el-descriptions class="margin-top " :contentStyle="CS" :label-style="LS" title="" :column="2" size="medium"
                       border>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-c-scale-to-original"></i>
            客户名称
          </template>
          {{ form.customerName }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-time"></i>
            税号
          </template>
          {{ form.taxNo }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-money"></i>
            开票金额
          </template>
          {{ form.amount }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-money"></i>
            申请时间
          </template>
          {{ parseTime(form.invoiceApplyDate, '{y}-{m}-{d} {h}:{i}:{s}') }}
        </el-descriptions-item>

        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-c-scale-to-original"></i>
            订单编号
          </template>
          {{ form.projectNo }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-bank-card"></i>
            开票明细
          </template>
          <file-upload-show :value="form.files"></file-upload-show>
        </el-descriptions-item>
      </el-descriptions>

      <!--申请明细-->
      <goods-invoice-apply-detail-list-section :applyId="applyId"></goods-invoice-apply-detail-list-section>

      <el-tabs class="detail-tab" type="card" v-model="activeName">
        <el-tab-pane label="开票信息" name="first">
          <el-row :gutter="24">
            <el-col :span="12">
              <el-form-item label="开票日期" prop="invoiceDate">
                <el-date-picker clearable
                                placeholder="请选择开票日期"
                                type="date"
                                v-model="form.invoiceDate"
                                value-format="yyyy-MM-dd">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="发票编号" prop="no">
                <el-input placeholder="请输入发票编号" v-model="form.no"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="开票金额" prop="invoiceMoney">
                <el-input-number :max="form.amount" :precision="2" placeholder="请输入开票金额" disabled
                                 v-model="form.amount"/>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="发票文件" prop="invoiceFile">
                <file-upload-invoice v-model="form.invoiceFile" @getInvoiceInfo="getInvoiceInfo"/>
              </el-form-item>
            </el-col>
          </el-row>
        </el-tab-pane>
      </el-tabs>
    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {addAction, getAction, getById, isEmpty, updateAction} from '@/api/manage'
import {updateInvoice} from '@/api/develop/goodsInvoiceBatch'
import GoodsInvoiceApplyDetailList from "@/views/develop/goodsInvoiceApplyDetail/GoodsInvoiceApplyDetailList.vue";
import GoodsInvoiceApplyDetailListSection
  from "@/views/develop/goodsInvoiceApplyDetail/GoodsInvoiceApplyDetailListSection.vue";
import FileUploadShow from "@/components/FileUpload/FileUploadShow.vue";
import {parseTime} from "@/utils/common";

export default {
  name: "GoodsInvoiceOpenForm",
  components: {FileUploadShow, GoodsInvoiceApplyDetailListSection, GoodsInvoiceApplyDetailList},
  dicts: ['goods_supplier', 'goods_customer', 'fund_callback', 'yes_no', 'company', 'goods_customer',
    'invoice_back_status'
  ],
  data() {
    return {
      // 遮罩层
      loading: true,
      activeName: "first",
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      columns: [],
      dataList: [],
      // 表单校验
      rules: {
        customerId: [{
          required: true,
          message: "客户名称不能为空",
          trigger: "blur"
        }],
      },
      total: 0,
      CS: {
        'text-align': 'center',  //文本居中
        'min-width': '250px',   //最小宽度
        'word-break': 'break-all'  //过长时自动换行
      },
      LS: {
        'color': '#000',
        'text-align': 'center',
        'font-weight': '600',
        'height': '40px',
        'min-width': '110px',
        'word-break': 'keep-all'
      },
      url: {
        getById: '/goodsInvoice/goodsInvoice/',
        add: '/goodsInvoice/goodsInvoice/add',
        openInvoice: '/goodsInvoice/goodsInvoice/openInvoice',
      },
      queryParams: {
        id: null,
        no: null,
        amount: null,
        amountReal: null,
        remaining: null,
        customerId: null,
        customerName: null,
        taxNo: null,
        invoiceCompany: null,
        fundCallback: null,
        backDate: null,
        drawerId: null,
        drawerName: null,
        saleId: null,
        saleName: null,
        files: null,
        status: "0",
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      },
      showEditBtn: true,
      taxNo: "",
      applyId: null,
    };
  },
  created() {

  },
  methods: {

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        no: null,
        amount: null,
        amountReal: null,
        remaining: null,
        customerId: null,
        customerName: null,
        taxNo: null,
        invoiceDate: null,
        invoiceCompany: null,
        fundCallback: null,
        backDate: null,
        drawerId: null,
        drawerName: null,
        saleId: this.$store.getters.userId,
        saleName: this.$store.getters.nickName,
        files: null,
        invoiceFile: null,
        status: "0",
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        deptId: this.$store.getters.deptId,
        deptName: this.$store.getters.deptName,
      };
      this.columns = [];
      this.showEditBtn = true;
      this.resetForm("form");
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加发票管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      let that = this;
      that.title = "开票";
      that.open = true;
      this.applyId = row.id;
      that.reset();
      that.showEditBtn = false;
      const id = row.id || that.ids;
      getById(that.url.getById, id).then(response => {
        that.form = response.data;
        this.form.invoiceDate = parseTime(new Date(), '{y}-{m}-{d} {h}:{i}:{s}')
      });
    },

    getInvoiceInfo(info) {
      if (!isEmpty(this.form.no)) {
        this.form.no += ",";
      } else {
        this.form.no = "";
      }
      this.form.no += info.number;
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAction(this.url.openInvoice, this.form).then(response => {
              this.$modal.msgSuccess("开票成功");
              this.open = false;
              this.$emit('ok');
            });
          } else {
            addAction(this.url.add, this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.$emit('ok');
            });
          }
        }
      });
    },
  }
};
</script>
